package com.ray.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.ray.auth.system.service.DictService;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Menu;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.zjhc.flyhong.redis.DefaultRedisService;



public class DictLoadUtil {
 /**字典值***/	
 private  Map<String,List<Dict>> dictMap = new HashMap<String,List<Dict>>();
 public  String NAME = "dicts";
 public  static String SECURITY_DICT = "security_dict";
 
 @Autowired
 private DefaultRedisService defaultRedisService;
 
 public  Map<String, List<Dict>> loadDict(DictService dictService, String... dictTypes) {
	//缓存
	Map<String,List<Dict>> result = new HashMap<String,List<Dict>>();
	if(ObjectUtil.isNotNull(dictTypes)){
		for(String type:dictTypes){
			String key=SECURITY_DICT+type;
			List list =defaultRedisService.get(key,List.class);
			if(list == null || list.size() == 0){
				list = dictService.queryDictsByDictType(type);
				defaultRedisService.set(key, list, 24*3600);
			}
			result.put(type, list);
		}
	}
	
	return result;
}
	/**
	 * 添加菜单
	 * @param list
	 */
	public  void addMenuDict(List<Menu> menuList,String name) {
		
		//List<Dict> list = dictMap.get(name);
		
		String key=SECURITY_DICT+name;
		List list =defaultRedisService.get(key,List.class);
		if(list == null || list.size() == 0){
			List<Dict> l = new ArrayList<Dict>();
			Dict dict = null;
			for(Menu menu:menuList){
				dict = new Dict();
				dict.setName(menu.getMenuName());
				dict.setValue(menu.getMenuId());
				l.add(dict);
			}
			dictMap.put(name, l);
			defaultRedisService.set(key, l, 24*3600);
		}
	}
}
